/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package objects;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.LoopMode;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import fpsman.MyMotionControl;
import fpsman.MyPath;

/**
 *
 * @author André
 */
public class Ghost {
    Spatial model;

    public Spatial getModel() {
        return model;
    }

    public void setModel(Spatial model) {
        this.model = model;
    }
    
    public RigidBodyControl getBody(){
        return model.getControl(RigidBodyControl.class);
    }
    
    public Ghost(String name, Vector3f position, AssetManager asset){
        model = asset.loadModel("Models/Ninja/Ninja.mesh.xml");
        model.scale(0.01f, 0.01f, 0.01f);
        model.setLocalTranslation(position);
        model.setName(name);
        RigidBodyControl body = new RigidBodyControl(1.5f);//CollisionShapeFactory.createMeshShape(model),1.5f);
        model.addControl(body);
        body.setCollisionGroup(2);
        body.removeCollideWithGroup(2);
        body.setPhysicsLocation(position);
        AnimChannel ch = model.getControl(AnimControl.class).createChannel();
        ch.setAnim("Walk");
        ch.setLoopMode(LoopMode.Loop);
    }
    
    public void update(){
        if(!getBody().isActive()) getBody().setEnabled(false);
        if(!getBody().isEnabled()) getBody().setEnabled(true);
        MyMotionControl m = getModel().getControl(MyMotionControl.class);
        getBody().setPhysicsLocation(m.getLocation());
    }
    
    public void resetPath(MyPath path){
        MyMotionControl m = getModel().getControl(MyMotionControl.class);
        m.setReseted(true);
        m.stop();
        m.attachPath(path);
        m.play();
    }
}
